package com.hjx.offer;

import java.util.ArrayList;
import java.util.List;

public class BinaryTreeNode<T> {

    int level;
    T value;
    BinaryTreeNode left;
    BinaryTreeNode right;
    BinaryTreeNode parent;

    public BinaryTreeNode(T value) {
        this.value = value;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public BinaryTreeNode getLeft() {
        return left;
    }

    public void setLeft(BinaryTreeNode left) {
        this.left = left;
    }

    public BinaryTreeNode getRight() {
        return right;
    }

    public void setRight(BinaryTreeNode right) {
        this.right = right;
    }

    public BinaryTreeNode getParent() {
        return parent;
    }

    public void setParent(BinaryTreeNode parent) {
        this.parent = parent;
    }

    public void widthFirst(){
        if (this.value == null) {
            return;
        }
        List<BinaryTreeNode> sb = new ArrayList<BinaryTreeNode>();
        sb.add(this);
        int initLevel = this.getLevel();
        while(sb.size() != 0) {
            BinaryTreeNode first = sb.remove(0);
            if (first.getLevel() != initLevel) {
                System.out.print("\n " + first.value);
                initLevel = first.getLevel();
            } else {
                System.out.print(" " + first.value);
            }


            if(first.left != null) {
                sb.add(first.left);
//            System.out.println(node.left.value + "\n");
            }
            if(first.right != null) {
                sb.add(first.right);
//            System.out.println(node.right.value + "\n");
            }
        }
        System.out.println();

    }
}
